package cn.onepiece.smili.util;

/**
 * @Author: haoting.wang
 * @Date: Created in 下午3:54 2017/5/25
 */

import java.io.IOException;
import java.sql.*;

import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;

public class JdbcUtil {

    public static Connection getConnection() {

        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/foundation?user=root&password=root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public void getTableMeta() throws SQLException {
        Connection connection = getConnection();

        connection.getMetaData();
    }

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/foundation?user=root&password=root");

        DatabaseMetaData metaData = connection.getMetaData();

        ResultSet u_user = metaData.getColumns(null, "%", "u_user", "%");

        while (u_user.next()) {
            String tableName=u_user.getString("TABLE_NAME");
            String remarkes = u_user.getString("REMARKS");
            String type = u_user.getString("DATA_TYPE");
            String TYPE_NAME = u_user.getString("TYPE_NAME");

            String column = u_user.getString("COLUMN_NAME");
            System.out.println(tableName+"="+column+type+remarkes+TYPE_NAME);
        }

    }


    public static void freemarker() throws IOException {

        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath("classpath*:/templates/");
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate("model.ftl");

        System.out.println(template);
    }

}
